package cn.xiaoyaoji.plugin.html;

import java.io.File;
import java.io.FileFilter;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class GroupConvertor
{
  private String groupName;
  private int groupIndex;
  private boolean ready;
  List<XmlConvertor> xmlConverts = new ArrayList();
  
  public GroupConvertor(int index)
  {
    this.groupIndex = index;
    this.ready = false;
  }
  
  public boolean convert(List<String> list)
  {
    this.ready = false;
    this.xmlConverts.clear();
    //File[] files = listXmlFiles(folder);
    int index = 0;
    for (int i = 0; i < list.size(); i++)
    {
      XmlConvertor convertor = new XmlConvertor(this.groupIndex, index);
      if (convertor.convert(list.get(i)))
      {
        this.xmlConverts.add(convertor);
        index++;
        this.ready = true;
      }
    }
    return this.ready;
  }
  
  public void setGroupName(String groupName)
  {
    this.groupName = groupName;
  }
  
  private File[] listXmlFiles(File folder)
  {
    File[] files = folder.listFiles(new FileFilter()
    {
      public boolean accept(File file)
      {
        if (!file.isDirectory())
        {
          String name = file.getName();
          int dotPos = name.lastIndexOf('.');
          if (dotPos != -1)
          {
            String suffix = name.substring(dotPos);
            if (".xml".equalsIgnoreCase(suffix)) {
              return true;
            }
          }
        }
        return false;
      }
    });
    return files;
  }
  
  public String toHtmlString()
  {
    if (!this.ready) {
      return "";
    }
    String templateString = HtmlTemplates.getInstance().getGroupTemplate();
    templateString = templateString.replace("${groupindex}", String.valueOf(this.groupIndex + 1));
    templateString = templateString.replace("${groupname}", this.groupName);
    templateString = templateString.replace("${items}", getItemsString());
    return templateString;
  }
  
  public String toDirHtmlString()
  {
    if (!this.ready) {
      return "";
    }
    String templateString = HtmlTemplates.getInstance().getDirGroupTemplate();
    templateString = templateString.replace("${groupindex}", String.valueOf(this.groupIndex + 1));
    templateString = templateString.replace("${groupname}", this.groupName);
    templateString = templateString.replace("${dir_items}", getDirItemsString());
    return templateString;
  }
  
  private String getItemsString()
  {
    StringBuilder sBuilder = new StringBuilder();
    for (XmlConvertor convertor : this.xmlConverts) {
      try
      {
        sBuilder.append(convertor.toHtmlString());
      }
      catch (Exception e)
      {
        System.out.println("xml irregular：" + convertor.getName());
        e.printStackTrace();
      }
    }
    return sBuilder.toString();
  }
  
  private String getDirItemsString()
  {
    StringBuilder sBuilder = new StringBuilder();
    for (XmlConvertor convertor : this.xmlConverts) {
      sBuilder.append(convertor.toDirHtmlString());
    }
    return sBuilder.toString();
  }
}
